<?php

class Application_Model_TicketMapper
{
	protected $_dbTable;

	public function setDbTable($dbTable)
	{
		if (is_string($dbTable)) {
			$dbTable = new $dbTable();
		}
		if (!$dbTable instanceof Zend_Db_Table_Abstract) {
			throw new Exception('Invalid table data gateway provided');
		}
		$this->_dbTable = $dbTable;
		return $this;
	}
    
	public function getDbTable()
	{
		if (null === $this->_dbTable) {
			$this->setDbTable('Application_Model_DbTable_Ticket');
		}
		return $this->_dbTable;
	}
	
	public function anlegen(Application_Model_Ticket $ticket)
	{
		$mysqlverfuegbarab = date('Y-m-d', strtotime($ticket->getVerfuegbar_ab()) );
		$mysqlverfuegbarbis = date('Y-m-d', strtotime($ticket->getVerfuegbar_bis()) );
		$mysqlgueltigab = date('Y-m-d', strtotime($ticket->getGueltig_ab()) );
		$mysqlgueltigbis = date('Y-m-d', strtotime($ticket->getGueltig_bis()) );
		$data = array(
			'ticketNr' 		=> $ticket->getNr(),
            'dauer'   		=> $ticket->getDauer(),
            'ticketName' 	=> $ticket->getName(),
            'preis' 		=> $ticket->getPreis(),
            'ticketart' 	=> $ticket->getTicketart(),
			'verfuegbar_ab' => $mysqlverfuegbarab,
			'verfuegbar_bis'=> $mysqlverfuegbarbis,
			'gueltig_ab' 	=> $mysqlgueltigab,
			'gueltig_bis' 	=> $mysqlgueltigbis,
			'tage' 			=> serialize($ticket->getTage())
		);

		$this->getDbTable()->insert($data);
	}
	public function ticketExists($nr=null){
		$ergebnis;
		$table = $this->getDbTable();
		$tempnr = $table->fetchAll($table->select('ticketNr')
     						->where('ticketNr = ?',$nr)); 	
		
     	if (sizeof($tempnr) >0) {
			$ergebnis = true;
		}else{
			$ergebnis= false;
		}
		return $ergebnis;
	}
	public function getTicketsAktuell() {
		$datum = date('Y-m-d');
		$table = $this->getDbTable();
		$resultSet = $table->fetchAll($table->select()
     							->where('verfuegbar_ab <= ?',$datum)
     							->where('verfuegbar_bis >= ?', $datum));
		$entries = array();
		foreach ($resultSet as $row) {
			$entry = new Application_Model_Ticket();
     		$entry->setNr($row->ticketNr);
     		$entry->setDauer($row->dauer);
     		$entry->setName($row->ticketName);
     		$entry->setPreis($row->preis);
     		$entry->setVerfuegbar_ab($row->verfuegbar_ab);
     		$entry->setVerfuegbar_bis($row->verfuegbar_bis);
     		$entry->setGueltig_ab($row->gueltig_ab);
     		$entry->setGueltig_bis($row->gueltig_bis);
     		$entry->setTicketart($row->ticketArt);
     		$entry->setTage(unserialize($row->tage));

     		$entries[] = $entry;
		}
		return $entries;
	}
	public function getTicket($nr) 
	{
		$table = $this->getDbTable();
		$resultSet = $table->fetchAll($table->select()
     							->where('ticketNr = ?',$nr));
		foreach ($resultSet as $row) {
			$entry = new Application_Model_Ticket();
     		$entry->setNr($row->ticketNr);
     		$entry->setDauer($row->dauer);
     		$entry->setName($row->ticketName);
     		$entry->setPreis($row->preis);
     		$entry->setVerfuegbar_ab($row->verfuegbar_ab);
     		$entry->setVerfuegbar_bis($row->verfuegbar_bis);
     		$entry->setGueltig_ab($row->gueltig_ab);
     		$entry->setGueltig_bis($row->gueltig_bis);
     		$entry->setTicketart($row->ticketArt);
     		$entry->setTage(unserialize($row->tage));

     		return $entry;
		}
	}
	
	public function  fetchAll()
	{
     	$resultSet = $this->getDbTable()->fetchAll();
     	$entries = array();
     	foreach ($resultSet as $row) {
     		$entry = new Application_Model_Ticket();
     		$entry->setNr($row->ticketNr);
     		$entry->setDauer($row->dauer);
     		$entry->setName($row->ticketName);
     		$entry->setPreis($row->preis);
     		$entry->setVerfuegbar_ab($row->verfuegbar_ab);
     		$entry->setVerfuegbar_bis($row->verfuegbar_bis);
     		$entry->setGueltig_ab($row->gueltig_ab);
     		$entry->setGueltig_bis($row->gueltig_bis);
     		$entry->setTicketart($row->ticketArt);
     		$entry->setTage(unserialize($row->tage));
     		
     		$entries[] = $entry;
     	}
     	return $entries;
	}
	public function loeschen($nr)
	{
		$this->getDbTable()->delete(array('ticketNr = ?' => $nr));
	}
	public function details($nr)
	{
		$table = $this->getDbTable();
		$resultSet = $table->fetchAll($table->select()
     							->where('ticketNr = ?',$nr));
     	foreach ($resultSet as $row) {
     	$detail = new Application_Model_Ticket();
     	$detail->setNr($row->ticketNr);
     	$detail->setDauer($row->dauer);
     	$detail->setName($row->ticketName);
     	$detail->SetPreis($row->preis);
     	$detail->SetVerfuegbar_ab($row->verfuegbar_ab);
     	$detail->SetVerfuegbar_bis($row->verfuegbar_bis);
     	$detail->setGueltig_ab($row->gueltig_ab);
     	$detail->setGueltig_bis($row->gueltig_bis);
     	$detail->setTicketart($row->ticketArt);
     	$detail->setTage(unserialize($row->tage));
     	return $detail;
     	}
	}
}
